package project.web;

import Utils.data.DataConverters;
import Utils.web.servlet.BaseServlet;
import Utils.web.WebUtils;
import project.pojo.Book;
import project.pojo.Page;
import project.service.BookService;
import project.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author d11yu
 */
@WebServlet(name = "ClientBookServlet", value = "/ClientBookServlet")
public class ClientBookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        int pn = DataConverters.parseInt(request.getParameter("pn"), 1);
        int ecp = DataConverters.parseInt(request.getParameter("ecp"), Page.ENTRY_COUNT_PER_PAGE);
        Page<Book> page = bookService.getPage(pn, ecp, "ClientBookServlet?action=page");
        request.setAttribute("page", page);
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    protected void pageByPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        int pn = DataConverters.parseInt(request.getParameter("pn"), 1);
        int ecp = DataConverters.parseInt(request.getParameter("ecp"), Page.ENTRY_COUNT_PER_PAGE);
        double minPrice = DataConverters.parseDouble(request.getParameter("minPrice"), 0);
        double maxPrice = DataConverters.parseDouble(request.getParameter("maxPrice"), Double.MAX_VALUE);
        StringBuilder sb = new StringBuilder("ClientBookServlet?action=pageByPrice");
        sb.append("&minPrice=").append(minPrice);
        sb.append("&maxPrice=").append(maxPrice);
        Page<Book> page = bookService.getPageByPrice(pn, minPrice, maxPrice, ecp, sb.toString());
        request.setAttribute("page", page);
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = WebUtils.copyParamsToBean(request.getParameterMap(), Book.class);
        String method = request.getParameter("action");
        String pn = request.getParameter("pn");
        bookService.addBook(book);
//        request.getRequestDispatcher("/manager/BookServlet?action=page&pn="+request.getParameter("pn")).forward(request, response); // 此方法会有重新提交的问题（在浏览器按F5）
        response.sendRedirect(request.getContextPath() + "/pages/manager/book_edit.jsp?method=" + method + "&pn=" + pn);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int pn = Integer.parseInt(request.getParameter("pn"));
        bookService.deleteBookById(id);
        response.sendRedirect(request.getContextPath() + "/manager/BookServlet?action=page&pn=" + pn);
    }

    protected void updateById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Book book = WebUtils.copyParamsToBean(request.getParameterMap(), Book.class);
        int pn = Integer.parseInt(request.getParameter("pn"));
        bookService.updateBookById(book);
        response.sendRedirect(request.getContextPath() + "/manager/BookServlet?action=page&pn=" + pn);
    }

    protected void queryBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        int id = Integer.parseInt(request.getParameter("id"));
        Book book = bookService.queryBookById(id);
        request.setAttribute("book", book);
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request, response);
    }

    protected void queryAllBooks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List<Book> books = bookService.queryAllBooks();
        request.setAttribute("books", books);
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }
}
